package com.spring.cootask.web.service.impl;

import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.cootask.web.dao.*;
import com.spring.cootask.web.model.CooTaskHead;
import com.spring.cootask.web.model.CooTaskTable;
import com.spring.cootask.web.model.CooTaskTrans;
import com.spring.cootask.web.model.CooTaskUser;
import com.spring.cootask.web.service.CooTaskService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WoJunyu on 2016/9/6.
 */
@Service
public class CooTaskServiceImpl implements CooTaskService{

    @Resource
    private CooTaskHeadMapper cooTaskHeadMapper;

    @Resource
    private CooTaskUserMapper cooTaskUserMapper;
    
    @Resource
    private CooTaskTableMapper cooTaskTableMapper;

    @Override
    public List<CooTaskHead> selectCreateTaskByParm(CooTaskHead data) {
        return cooTaskHeadMapper.selectCreateTaskByParm(data);
    }

    @Override
    public List<CooTaskTrans> selectTransTaskByParm(CooTaskTrans data) {
        return cooTaskHeadMapper.selectTransTaskByParm(data);
    }

    @Override
    public BaseResult saveTask(CooTaskHead data, ShopOaSessionUtil.OaUser oaUser)  throws Exception{
        BaseResult ret = new BaseResult();
        if(oaUser==null||!ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
            ret.setStatus("0");
            ret.setMsg("请先登录");
            return ret;
        }
        int i;
        if(ApplicationUtils.isNotNull(data.getId())){//修改
            data.setChangedtime(new Date());
            data.setOperatorid(oaUser.getOa_user_id());
            data.setOperatorname(oaUser.getOa_user_name());
            i = cooTaskHeadMapper.updateByPrimaryKeySelective(data);
            //插入扩展表            
            cooTaskTableMapper.deleteByHeadID(data.getId());

            Map<String,Object> param=new HashMap<String,Object>();
            param.put("headID", data.getId());
            param.put("tablebaseID", data.getTablebaseID());

            cooTaskTableMapper.insertByTablesID(param);                       
        }else{//新增
            String id = ApplicationUtils.randomUUID();
            data.setId(id);
            data.initInfo();
            data.setTaskStatus(0);
            data.setCreateUserId(oaUser.getOa_user_id());
            data.setCreateUserName(oaUser.getOa_user_name());
            data.setCreateDeptId(oaUser.getUser_dept_id());
            data.setCreateDeptName(oaUser.getUser_dept_name());
            data.setInitialoperatorid(oaUser.getOa_user_id());
            data.setInitialoperatorname(oaUser.getOa_user_name());
            i = cooTaskHeadMapper.insert(data);
            
            if(data.getTablebaseID()!=null&&!data.getTablebaseID().equals("")){
                //插入扩展字段
                Map<String,Object> param=new HashMap<String,Object>();
                param.put("headID", id);
                param.put("tablebaseID", data.getTablebaseID());

                cooTaskTableMapper.insertByTablesID(param);
            }
            
            //增加第一级流转
            CooTaskUser record = new CooTaskUser();
            record.setToDeptId(oaUser.getUser_dept_id());
            record.setToDeptName(oaUser.getUser_dept_name());
            record.setToUserId(oaUser.getOa_user_id());
            record.setToUserName(oaUser.getOa_user_name());
            record.setfTaskHeadid(id);
            record.setTransferLevel(0);
            record.setNote("任务发起");
            record.setStatus(10);//发起;
            saveTaskUser(record, oaUser);
        }
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("保存失败");
        }
        return ret;
    }

    @Override
    public BaseResult deleteTask(String id) throws Exception{
        BaseResult ret = new BaseResult();
        CooTaskHead up = new CooTaskHead();
        up.setId(id);
        up.setEntityavailablestate(20);
        int i = cooTaskHeadMapper.updateByPrimaryKeySelective(up);
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("删除失败");
        }
        return ret;
    }

    @Override
    public BaseResult saveTaskUser(CooTaskUser data, ShopOaSessionUtil.OaUser oaUser) throws Exception {
        BaseResult ret = new BaseResult();
        data.initInfo();
        data.setInitialoperatorid(oaUser.getOa_user_id());
        data.setInitialoperatorname(oaUser.getOa_user_name());
        String[] userIds = data.getToUserId().split(",");
        String[] userNames = data.getToUserName().split(",");
        String[] deptIds = data.getToDeptId().split(",");
        String[] deptNames = data.getToDeptName().split(",");
        int retI = 0;
        for(int i=0;i<userIds.length;i++){
            data.setId(ApplicationUtils.randomUUID());//重新设置流转id
            data.setToUserId(userIds[i]);
            data.setToUserName(userNames[i]);
            data.setToDeptId(deptIds[i]);
            data.setToDeptName(deptNames[i]);
            retI += cooTaskUserMapper.insert(data);
        }

        if(retI==0){
            ret.setStatus("0");
            ret.setMsg("下发失败");
        }else{
            ret.setMsg("下发成功");
        }
        return ret;
    }

    @Override
    public BaseResult updateTaskUser(CooTaskUser data, ShopOaSessionUtil.OaUser oaUser) throws Exception {
        BaseResult ret = new BaseResult();
        data.setChangedtime(new Date());
        data.setOperatorid(oaUser.getOa_user_id());
        data.setOperatorname(oaUser.getOa_user_name());
        if(data.getId()!=null){
        	 int i = cooTaskUserMapper.updateByPrimaryKeySelective(data);
        	 if(i==0){
                 ret.setStatus("0");
                 ret.setMsg("状态更新失败");
             }
        }
        return ret;
    }

}
